package com.myrabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class FanoutProductor {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("129.211.131.113");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        try {
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();

            String exchangeName = "test_fanout_exchange";
//            String routingKey1 = "test.topic.key1";
//            String routingKey2 = "test.topic.key2";
//            String routingKey3 = "test.topic.key3";

                //发送数据
                String msg = "Hello RabbitMQ fanout" ;
                channel.basicPublish(exchangeName,"",null,(msg).getBytes());
//                channel.basicPublish(exchangeName,routingKey2,null,(msg+routingKey2).getBytes());
//                channel.basicPublish(exchangeName,routingKey3,null,(msg+routingKey3).getBytes());


            //关闭连接
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
